/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eit.ntc.lmsbackend.struts.action;

import com.eit.ntc.lmsbackend.struts.form.MetaDataForm;
import com.eit.ntc.struts.action.AbstractWebFlowAction;
import com.eit.toschoolonline.lms.jdo.MetaDataMasterTypeM;
import com.eit.toschoolonline.lms.service.MetaDataMasterService;
import java.util.Iterator;
import org.springframework.validation.Errors;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;

/**
 *
 * @author oxopro
 */
public class MetaDataAction extends AbstractWebFlowAction {

    private MetaDataMasterService metaDataMasterService;

    public MetaDataMasterService getMetaDataMasterService() {
        return metaDataMasterService;
    }

    public void setMetaDataMasterService(MetaDataMasterService metaDataMasterService) {
        this.metaDataMasterService = metaDataMasterService;
    }

    public Event getMetaDataMasterAction(RequestContext context) throws Exception {

        MetaDataForm form = (MetaDataForm) getFormObject(context);
        form.getMasterList().clear();
        form.setKeyword("");
        form.setValue("");
        form.setMasterList(getMetaDataMasterService().getMasterAll());

        return success();
    }

    public Event addMasterAction(RequestContext context) throws Exception {

        MetaDataForm form = (MetaDataForm) getFormObject(context);
        MetaDataMasterTypeM meta = new MetaDataMasterTypeM();
        Errors error = getFormErrors(context);

        if (form.getKeyword() == null || form.getKeyword().equals("")) {
            error.reject("metadata.keywordEmpty");
            return error();
        }

        Iterator it = form.getMasterList().iterator();
        while (it.hasNext()) {
            MetaDataMasterTypeM m = (MetaDataMasterTypeM) it.next();
            if (m.getKeyword().equals(form.getKeyword())) {
                error.reject("metadata.keywordDuplicate");
                return error();
            }
        }

        meta.setKeyword(form.getKeyword());
        meta.setDefaultValue(form.getKeyword());
        getMetaDataMasterService().createMaster(meta);

        return success();
    }

    public Event editMasterAction(RequestContext context) throws Exception {

        MetaDataForm form = (MetaDataForm) getFormObject(context);
        MetaDataMasterTypeM meta = new MetaDataMasterTypeM();
        meta = (MetaDataMasterTypeM) form.getMasterList().get(Integer.parseInt(form.getIndex()));

        form.setKeyword(meta.getKeyword());

        return success();
    }

    public Event updateMasterAction(RequestContext context) throws Exception {

        MetaDataForm form = (MetaDataForm) getFormObject(context);
        MetaDataMasterTypeM meta = new MetaDataMasterTypeM();
        Errors error = getFormErrors(context);

        if (form.getKeyword() == null || form.getKeyword().equals("")) {
            error.reject("metadata.keywordEmpty");
            return error();
        }

        meta = (MetaDataMasterTypeM) form.getMasterList().get(Integer.parseInt(form.getIndex()));
        meta.setKeyword(form.getKeyword());
        meta.setDefaultValue(form.getKeyword());
        getMetaDataMasterService().updateMaster(meta);

        return success();
    }

    public Event deleteMasterAction(RequestContext context) throws Exception {

        MetaDataForm form = (MetaDataForm) getFormObject(context);
        MetaDataMasterTypeM meta = new MetaDataMasterTypeM();
        meta = (MetaDataMasterTypeM) form.getMasterList().get(Integer.parseInt(form.getIndex()));
        getMetaDataMasterService().deleteMaster(meta);

        return success();
    }

    public Event getMetaDataAction(RequestContext context) throws Exception {

        MetaDataForm form = (MetaDataForm) getFormObject(context);
        form.getMetaList().clear();
        form.setKeyword("");
        form.setValue("");
        
        return success();
    }
}
